import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    adminMenu: [],
    user: {
      userName: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).userName
    }
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    login (state, result) {
      state.user = result.user
      Vue.ls.set('Access-Token', result.token, 7 * 24 * 60 * 60 * 1000)
      this.SET_TOKEN('SET_TOKEN', result.token)
    },
    logout (state) {
      state.user = []
      window.localStorage.removeItem('user')
    },
    initAdminMenu (state, menus) {
      state.adminMenus = menus
    }
  }
})
